/**
 * Created by lxy_920912 on 16/8/8.
 */
var mainAttrListModel = {
    data:[{
        "master_id":1,
        "master_name":"Goo",
        "type":"int",
        "length":18,
        "default_value":"fuckYOU",
        'vendor':'MD',
        'master_text':'GooHello',
        'tag':'tagy',
        "create_time":"2016-08-10 10:47:42"
    },
        {"master_id":2,
            "master_name":"Hello",
            "type":"string",
            "length":10,
            "default_value":"shit",
            'vendor':'MD',
            'master_text':'lxtHello',
            'tag':'tagx',
            "create_time":"2016-08-11 10:47:42"}],
    heading:{
        "序号":"50px",
        "属性名称":"80px",
        "显示名称":"80px",
        "标签":"80px",
        "厂商":"80px",
        "属性类型":'80px',
        "长度":'50px',
        "默认值":'60px',
        "编辑":'60px',
        "删除":'60px',
        "查看子属性":'100px'},
    attr:["","master_id","master_name","master_text","tag","vendor","type","length","default_value"],

    init:function () {//建立表头

        var oTable = document.getElementById('mainAttr');
        var oTrList = oTable.getElementsByTagName("tr");
        for(var i = oTrList.length-1;i>0;i--){
            oTrList[i].remove();
        }
        var caption= oTable.getElementsByTagName('caption');
        if(caption.length != 0){
            oTable.removeChild(caption[0]);
        }

        buildTable.init('mainAttr','主属性列表',mainAttrListModel.heading);

    },

    run:function () {//从数据库查询所有主属性
        mainAttrListModel.init();
        $.ajax({
            type:"GET",
            url: indexModel.url+"attr/list",
            dataType: "jsonp",
            jsonp: "callback",
            jsonpCallback:"data1",
            //data : master,
            success : function(json) {
                if (json) {
                    mainAttrListModel.setDataNo(json);//查询数据倒序
                    mainAttrListModel.showMasterList(mainAttrListModel.data);//建立主属性表格
                    indexModel.showDiv('mainAttrListDetail');//展示

                };
            },
            error: function() {
                console.log("查询所有主属性没有值!");
            }
        });
    },
    setDataNo:function (data) {
        data  = data.reverse();
        for(var i= 0;i < data.length;i++){
            data[i]['master_id'] = i+1;
        }
        mainAttrListModel.data = data;
    },
    showMasterList:function (data) {//展示所有主属性的表格
        for(var i in data){
            mainAttrListModel.addAttr(data[i]);
        }
        var oBtn= document.getElementById('addMainAttr');//添加新主属性
        oBtn.onclick = function () {
            var oForm = document.getElementById('newMainAttr');
            oForm.reset();//清空form
            var input = document.getElementById('master_id');
            input.value = "-1";
            indexModel.showDiv('mainAttrEditDetail');
        }
    },
    addAttr:function (data) {
        var oData="{";
        var attr = mainAttrListModel.attr;
        for(var i = 1;i < 9;i++){
            oData +="'"+attr[i]+"':'"+data[attr[i]]+"',";
        }
        oData+="}";

        data = eval("("+oData+")");
        buildTable.addTr('mainAttr',data);
        var oBtn= document.getElementById('addMainAttr');
        var otable = document.getElementById('mainAttr');
        var oTr = otable.getElementsByTagName("tr");


        mainAttrListModel.addBtn(oTr.length-1);
        oBtn.onclick = function () {
            var oForm = document.getElementById('newMainAttr');
            indexModel.showDiv('mainAttrEditDetail');
        }
    },
    addBtn:function (i) {
        var oTable = document.getElementById('mainAttr');
        var oTr =  oTable.getElementsByTagName('tr');

        var oTd = document.createElement("td");
        var oBtn = buildTable.createBtn("编辑",function () {
            //编辑按钮的监听事件
            var oFrom = document.getElementById('newMainAttr');
            var child = document.getElementsByClassName('input1');
            document.getElementById('master_name').disabled = true;

            var dataJson = mainAttrListModel.getTrAttr(this);
            for(var i in dataJson){
              //  alert("attr:"+i);
                var oInput = document.getElementById(i);
                oInput.value = dataJson[i];
            }
            indexModel.showDiv('mainAttrEditDetail');
        });
        oTd.appendChild(oBtn);
        oTr[i].appendChild(oTd);

        oTd = document.createElement('td');
        oBtn = buildTable.createBtn('删除',function () {
            var dataJson = mainAttrListModel.getTrAttr(this);
            var master_name = dataJson['master_name'];//属性名称从数据库删除
           // alert(attrName);
            var flag = confirm('是否确定主属性'+master_name+'删除?');
            if(flag){
                //删除操作   。。。。
                mainAttrListModel.deleteMaster(master_name);
             //var a= $(this).parent().parent().remove();

            }

        });
        oTd.appendChild(oBtn);
        oTr[i].appendChild(oTd);

        oTd = document.createElement('td');
        oBtn = buildTable.createBtn('查看',function () {
            var dataJson = mainAttrListModel.getTrAttr(this);
            var master_name = dataJson['master_name'];
            //根据Attr 从数据库中选择数据
            subAttrListModel.showSubList(master_name)//subAttrList table 建设和初始化
            indexModel.showDiv('subAttrListDetail')
        })
        oTd.appendChild(oBtn);
        oTr[i].appendChild(oTd);
    },
    getTrAttr:function (btn) {
        var pTr = $(btn).parent().parent().html();
        var data = pTr.split("<td>");

        var oData="{";
        var attr = mainAttrListModel.attr;
        for(var i= 1;i < 9;i++){
            data[i]= data[i].replace("</td>","");
            oData+="'"+attr[i]+"':'"+data[i]+"',";
        }
        oData+="}";
        var dataJson = eval("("+oData+")");
        return dataJson;
    },
    deleteMaster:function (master_name) {

        $.ajax({
            type:"GET",
            url: indexModel.url+"attr/delete",
            dataType: "jsonp",
            jsonp: "callback",
            jsonpCallback:"data1",
            data : "master_name="+master_name,
            success : function(json) {
                if (json=="0") {
                    //alert("删除成功!");
                    mainAttrListModel.data = json;
                    mainAttrListModel.run();
                    indexModel.showDiv('mainAttrListDetail');

                };
            },
            error: function() {
                console.log("tbl_getAttackSource没有值!");
            }
        });
    },

}